/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package cn.hxcx.top.modules.dvc.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.hxcx.top.common.config.Global;
import cn.hxcx.top.common.persistence.Page;
import cn.hxcx.top.common.utils.StringUtils;
import cn.hxcx.top.common.web.BaseController;
import cn.hxcx.top.modules.dvc.entity.DeviceInfo;
import cn.hxcx.top.modules.dvc.entity.DeviceMana;
import cn.hxcx.top.modules.dvc.service.DeviceInfoService;
import cn.hxcx.top.modules.dvc.service.DeviceManaService;
import cn.hxcx.top.modules.sys.utils.UserUtils;

/**
 * 设备报修Controller
 * @author DoubleX
 * @version 2019-07-13
 */
@Controller
@RequestMapping(value = "${adminPath}/dvc/deviceMana")
public class DeviceManaController extends BaseController {

	@Autowired
	private DeviceManaService deviceManaService;
	@Autowired
	private DeviceInfoService deviceInfoService;
	
	@ModelAttribute
	public DeviceMana get(@RequestParam(required=false) String id) {
		DeviceMana entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = deviceManaService.get(id);
		}
		if (entity == null){
			entity = new DeviceMana();
		}
		return entity;
	}
	
	@RequiresPermissions("dvc:deviceMana:view")
	@RequestMapping(value = {"list", ""})
	public String list(DeviceMana deviceMana, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<DeviceMana> page = deviceManaService.findPage(new Page<DeviceMana>(request, response), deviceMana); 
		model.addAttribute("page", page);
		model.addAttribute("pagetype", "0");
		return "modules/dvc/deviceManaList";
	}
	
	@RequiresPermissions("dvc:deviceMana:view")
	@RequestMapping(value = {"myputlist"})
	public String myputlist(DeviceMana deviceMana, HttpServletRequest request, HttpServletResponse response, Model model) {
		deviceMana.setCreateBy(UserUtils.getUser());
		Page<DeviceMana> page = deviceManaService.findPage(new Page<DeviceMana>(request, response), deviceMana); 
		model.addAttribute("page", page);
		model.addAttribute("pagetype", "1");
		return "modules/dvc/deviceManaList";
	}
	
	@RequiresPermissions("dvc:deviceMana:view")
	@RequestMapping(value = {"mymanalist"})
	public String mymanalist(DeviceMana deviceMana, HttpServletRequest request, HttpServletResponse response, Model model) {
		deviceMana.setRepairId(UserUtils.getUser());
		Page<DeviceMana> page = deviceManaService.findPage(new Page<DeviceMana>(request, response), deviceMana); 
		model.addAttribute("page", page);
		model.addAttribute("pagetype", "2");
		return "modules/dvc/deviceManaList";
	}

	@RequiresPermissions("dvc:deviceMana:view")
	@RequestMapping(value = "form")
	public String form(DeviceMana deviceMana, Model model,RedirectAttributes redirectAttributes) {
		if(StringUtils.isNotBlank(deviceMana.getDeviceId()) && deviceManaService.hasSameDevice(deviceMana.getDeviceId())) {
			addMessage(redirectAttributes, "该设备已报修");
			return "redirect:"+Global.getAdminPath()+"/dvc/deviceUse/mylist?repage";
		}
		model.addAttribute("deviceMana", deviceMana);
		return "modules/dvc/deviceManaForm";
	}
	
	@RequiresPermissions("dvc:deviceMana:view")
	@RequestMapping(value = "fixform")
	public String fixform(DeviceMana deviceMana, Model model,RedirectAttributes redirectAttributes) {
		model.addAttribute("deviceMana", deviceMana);
		return "modules/dvc/deviceManaForm";
	}
	
	@RequiresPermissions("dvc:deviceMana:view")
	@RequestMapping(value = "detail")
	public String detail(DeviceMana deviceMana, Model model) {
		model.addAttribute("deviceMana", deviceMana);
		return "modules/dvc/deviceManaDetail";
	}
	
	@RequiresPermissions("dvc:deviceMana:edit")
	@RequestMapping(value = "startfix")
	public String startfix(DeviceMana deviceMana, Model model, RedirectAttributes redirectAttributes) {
		deviceMana.setProgress("2");
		deviceMana.setRepairId(UserUtils.getUser());
		deviceMana.setMaintainDate(new Date());
		deviceManaService.save(deviceMana);
		addMessage(redirectAttributes, "已开始维修");
		return "redirect:"+Global.getAdminPath()+"/dvc/deviceMana/?repage";
	}
	
	@RequiresPermissions("dvc:deviceMana:edit")
	@RequestMapping(value = "startRepair")
	public String startRepair(DeviceMana deviceMana, Model model, RedirectAttributes redirectAttributes) {
		if(StringUtils.isNotBlank(deviceMana.getDeviceId()) && deviceManaService.hasSameDevice(deviceMana.getDeviceId())) {
			addMessage(redirectAttributes, "该设备已在维修中");
			return "redirect:"+Global.getAdminPath()+"/dvc/deviceUse/mylist?repage";
		}
		DeviceInfo deviceInfo = deviceInfoService.get(deviceMana.getDeviceId());
		deviceInfo.setDeviceStatus("3");
		deviceInfoService.save(deviceInfo);
		deviceMana.setProgress("2");
		deviceMana.setMaintainDetail("设备维护");
		deviceMana.setRepairId(UserUtils.getUser());
		deviceMana.setMaintainDate(new Date());
		deviceManaService.save(deviceMana);
		addMessage(redirectAttributes, "已开始维修");
		return "redirect:"+Global.getAdminPath()+"/dvc/deviceInfo/?repage";
	}

	@RequiresPermissions("dvc:deviceMana:edit")
	@RequestMapping(value = "save")
	public String save(DeviceMana deviceMana, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, deviceMana)){
			return form(deviceMana, model,redirectAttributes);
		}
		if(StringUtils.isBlank(deviceMana.getProgress())) {
			//报修
			deviceMana.setProgress("0");
			deviceManaService.save(deviceMana);
			addMessage(redirectAttributes, "设备报修成功");
			return "redirect:"+Global.getAdminPath()+"/dvc/deviceMana/myputlist?repage";
		}else if(deviceMana.getProgress().equals("2")) {
			//维修反馈
			DeviceInfo deviceInfo = deviceInfoService.get(deviceMana.getDeviceId());
			if(deviceInfo.getDeviceStatus().equals("3")) {
				deviceInfo.setDeviceStatus("1");
			}
			deviceInfoService.save(deviceInfo);
			deviceMana.setProgress("3");
			deviceMana.setRepairDate(new Date());
			deviceManaService.save(deviceMana);
			addMessage(redirectAttributes, "设备维修完成");
			return "redirect:"+Global.getAdminPath()+"/dvc/deviceMana/mymanalist?repage";
		}else {
			addMessage(redirectAttributes, "状态保存失败");
			return "redirect:"+Global.getAdminPath()+"/dvc/deviceMana/?repage";
		}
	}
	
	@RequiresPermissions("dvc:deviceMana:edit")
	@RequestMapping(value = "delete")
	public String delete(DeviceMana deviceMana, RedirectAttributes redirectAttributes) {
		deviceManaService.delete(deviceMana);
		addMessage(redirectAttributes, "撤销报修成功");
		return "redirect:"+Global.getAdminPath()+"/dvc/deviceMana/?repage";
	}

}